package com.projectName.www.dao;

import com.projectName.www.po.orders;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;

public class Y_J_buycar_dao {
    //返回加入购物车的菜单
    public static  ResultSet filldishe(String name) throws Exception {
        String sql="select * from orders where bool_buyer=1 and bool_shop=0 and Buyers=?";
        ResultSet resultSet =BaseDao.executeQuery(sql,name);
        return resultSet;
    }
    //返回客户的优惠券
    public static  ResultSet fillcoupon(String name ) throws Exception {
        String SQL ="select * from coupons where username = ?";
        ResultSet resultSet = BaseDao.executeQuery(SQL, name);
        return  resultSet;
    }
    //检查优惠券是否过期
    public static boolean isUse(String time){
        String finaltime = time + " 00:00:00";
        DateTimeFormatter df = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
        LocalDateTime l1 = LocalDateTime.parse(finaltime, df);
        LocalDateTime l2 = LocalDateTime.now();
        boolean after = l1.isAfter(l2);
        return  after;
    }
    //下单
    public static boolean makeorder(orders r) throws Exception {
        String sql =" update orders set bool_shop = 1 where Buyers=? and " +
                "business=? and dishename=? and money=?  and num=? and bool_shop = 0";
        int j;
        try{
            String Sql = " update user set money =money -? where id =0 and name =?";
            j = BaseDao.executeUpdata(Sql, r.getMoney(), r.getBuyers());
        }catch (SQLException e)
        {
            j=0;
        }
       int i= BaseDao.executeUpdata(sql,r.getBuyers(),r.getBusiness(),r.getDishename(),r.getMoney(),r.getNum());
       if(i>0&&j>0)return true;
       else return false;
    }
//清除购物车===Bool_buyer=0
    public static  boolean close(orders r) throws Exception {
        String sql =" update orders set bool_buyer = 0 where Buyers=? and " +
                "business=? and dishename=? and money=?  and num=?  and bool_shop = 0 ";
        int i= BaseDao.executeUpdata(sql,r.getBuyers(),r.getBusiness(),r.getDishename(),r.getMoney(),r.getNum());
        if(i>0)return true;
        else return false;

    }
}
